var { Action } = require("../../base/index");
var {
  randomUtils,
  timeUtils,
  swipeUtils,
  textUtils,
  ocrUtils,
  logUtils,
  clickOneContent,
} = require("../../utils/index");

var aRead = new Action("刷视频", {});
aRead.exec = function () {
  var span = 60;
  var fSpan = randomUtils.getRandom(span, span + 20);
  logUtils.info("本轮看视频预计", fSpan, "秒");
  this.startRead(fSpan);
};
aRead.startRead = function (span) {
  var startTime = Date.now();
  var count = 0;
  while (true) {
    count++;
    this.read(count);
    var hasExec = timeUtils.hasExec(startTime);
    if (hasExec >= span) {
      logUtils.info("将结束本轮看视频！！！", span, "/", hasExec);
      break;
    }
  }
};
aRead.read = function (count) {
  var span = randomUtils.getRandom(10, 25);
  var startTime = Date.now();
  logUtils.info("第", count, "次看视频，将看", span, "秒");
  while (true) {
    sleep(5 * 1000);
    if (this.checkZhiBoJian()) {
      sleep(randomUtils.getRandom(1.5, 3, 1000));
      logUtils.info("将滑动一次视频");
      swipeUtils.swipeVideo();
    }
    var hasExec = timeUtils.hasExec(startTime);
    if (hasExec > span) {
      logUtils.info("将结束第", count, "次看视频！！！", span, "/", hasExec);
      logUtils.info("将要滑动一次");
      swipeUtils.swipeVideo();
      break;
    }
  }
};
aRead.checkZhiBoJian = function () {
  var contents = ["点击进入", "直播间"];
  var exsitText = textUtils.existsOneTextStartsWith(contents);
  if (exsitText) {
    return exsitText;
  }
  var exsitOcr = ocrUtils.captureExistsOneText(contents);
  if (exsitOcr) {
    return exsitOcr;
  }
  var contents = ["以后再说"];
  if (clickOneContent(contents)) {
    sleep(randomUtils.getRandom(1.5, 3, 1000));
  }
  return false;
};

module.exports = aRead;
